import { type RouteRecordRaw, createRouter, createWebHashHistory, createWebHistory } from "vue-router"

const Layout = () => import("@/layout/index.vue")

// 常量路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    meta: {  },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },

  // 登录
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    name: "Login",
    hidden: true,
    meta: { title: "登录",  }

  },

  // 首页
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",

    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "工作台",
          svgIcon: "dashboard",
          lang: 'dashboard',
          affix: true
        }
      }
    ]
  },
  {
    path: '/low-code',
    component: Layout,
    key: 'lowCode',
    children: [
      {
        path: "low-code",
        component: () => import("@/views/lowCode/index.vue"),
        name: 'LowCode',
        key: 'lowCode',
        meta: {
          keepAlive: true,
          title: '低代码',
          svgIcon: 'lowCode',
          lang: 'lowCode',
        }
      }
    ]
  },

  {
    path: '/errorPage',
    component: Layout,

    redirect: "/errorPage/403",
    meta: { title: '错误页', lang: 'errPage', svgIcon: "errPages", hidden: true, },
    children: [
      {
        path: '403',
        name: '403',
        component: () => import("@/views/errorPages/403.vue"),
        meta: { title: '403', lang: '403' },

      }, {
        path: '404',
        name: '404',
        component: () => import("@/views/errorPages/404.vue"),
        meta: { title: '404', lang: '404' },
      }
    ]
  },
]



// 动态路由
export const asyncRoutes: RouteRecordRaw[] = [

  {
    path: "/conponents",
    component: Layout,
    redirect: "/components/selArea",
    key: 'Components',
    hidden: true, // 隐藏在侧边栏(需要权限)
    meta: { title: '组件示例', lang: 'conponents', svgIcon: 'component', },
    children: [
      {
        path: 'selArea',
        component: () => import("@/views/conponents/SelArea/index.vue"),
        name: 'SelArea',
        key: 'Sel-area',
        hidden: true,
        meta: { title: '省市区选择', lang: 'SelArea', svgIcon: 'selArea' },
      }, {
        path: 'uploadImage',
        component: () => import("@/views/conponents/uploadImage/index.vue"),
        name: 'UploadImage',
        key: 'Upload-image',
        hidden: true,
        meta: { title: '图片上传', lang: 'uploadImage', svgIcon: 'uploadImage' },
      },
      {
        path: 'watermark',
        component: () => import("@/views/conponents/watermark/index.vue"),
        name: 'Watermark',
        key: 'watermark',
        hidden: true,
        meta: { title: '水印', lang: 'watermark', svgIcon: 'watermark' },
      }
    ]
  },
  {
    path: "/table",
    component: Layout,
    redirect: "/table/vxe-table",
    // 隐藏在侧边栏(需要权限)
    hidden: true,
    key: 'table',
    meta: {
      title: "示例表格",
      elIcon: "Grid",
      lang: 'table',

    },
    children: [
      {
        path: "vxe-table",
        component: () => import("@/views/table/vxe-table/index.vue"),
        name: "VxeTable",
        // 隐藏在侧边栏(需要权限)
        key: 'vxe-table',
        meta: {
          title: "Vxe Table",
          keepAlive: true,
          svgIcon: "table",
          lang: 'vxeTable',
          hidden: true,
        }
      }, {
        path: "el-table",
        component: () => import("@/views/table/el-table/index.vue"),
        name: "ElTable",
        // 隐藏在侧边栏(需要权限)
        key: 'table-seal',
        meta: {
          title: "el-table",
          keepAlive: true,
          svgIcon: "table",
          lang: 'elTable',
          hidden: true,
        }
      },
    ]
  },
  {
    path: "/user",
    component: Layout,
    key: 'User',
    redirect: "/user/list",
    hidden: true, // 隐藏在侧边栏(需要权限)
    meta: {
      title: "用户管理",
      svgIcon: "user",
      lang: 'user',
    },
    children: [{
      path: "list",
      key: 'User-list',
      component: () => import("@/views/user/list/index.vue"),
      name: "UserList",
      hidden: true, // 隐藏在侧边栏(需要权限)
      meta: {
        title: "用户列表",
        keepAlive: true,
        svgIcon: "user",
        lang: 'userList',
      }
    },{
      path: "add",
      key: 'addUser',
      component: () => import("@/views/user/addUser/index.vue"),
      name: "AddUser",
      hidden: true, // 隐藏在侧边栏(需要权限)
      meta: {
        title: "新增用户",
        keepAlive: true,
        svgIcon: "addUser",
        lang: 'addUser',
      }
    }
    ]
  },
  {
    // 系统配置
    path: "/system",
    component: Layout,
    redirect: "/system/role",
    key: "System",
    hidden: true, // 隐藏在侧边栏(需要权限)
    meta: {
      title: "系统配置",
      elIcon: "el-icon-setting",
      lang: 'system',
    },
    children: [
      {
        path: "role",
        component: () => import("@/views/system/role/index.vue"),
        name: "Role",
        key: 'Role',
        hidden: true, // 隐藏在侧边栏(需要权限)
        meta: {
          title: "角色管理",
          keepAlive: true,
          svgIcon: "role",
          lang: 'role',

        }
      }, {
        path: "menu",
        component: () => import("@/views/system/menu/index.vue"),
        name: "Menu",
        hidden: true, // 隐藏在侧边栏(需要权限)
        key: 'Menu',
        meta: {
          title: "菜单管理",
          keepAlive: true,
          svgIcon: "menu",
          lang: 'menu',

        }
      }, {
        path: "dict",
        component: () => import("@/views/system/dict/index.vue"),
        name: "Dict",
        key: 'Dict',
        hidden: true, // 隐藏在侧边栏(需要权限)
        meta: {
          title: "字典管理",
          keepAlive: true,
          svgIcon: "dict",
          lang: 'dict',

        }
      }, {
        path: "config",
        component: () => import("@/views/system/config/index.vue"),
        name: "Config",
        key: 'Config',
        hidden: true, // 隐藏在侧边栏(需要权限)
        meta: {
          title: "参数设置",
          keepAlive: true,
          svgIcon: "config",
          lang: 'config',

        }
      },
    ]
  },
]



const router = createRouter({
  linkActiveClass: 'active',
  history:
    import.meta.env.VITE_ROUTER_HISTORY === "hash"
      ? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
      : createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
  routes: constantRoutes
})


/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router